package com.yun.zf.admin.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yun.zf.admin.mapper.ZfSchoolsMapper;
import com.yun.zf.admin.service.ZfSchoolsService;
import com.yun.zf.common.admin.domain.ZfSchools;
import com.yun.zf.common.admin.dto.community.CommunitySchoolsDto;
import com.yun.zf.common.admin.model.community.CommunitySchoolsMo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author cj
 * @since 2021-05-12
 */
@Service
public class ZfSchoolsServiceImpl extends ServiceImpl<ZfSchoolsMapper, ZfSchools> implements ZfSchoolsService {

    @Override
    @Transactional
    public int  addCommunitySchools(CommunitySchoolsMo schoolsMo) {
        ZfSchools schools = new ZfSchools();
        BeanUtil.copyProperties(schoolsMo, schools,  CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        schools.setCreateTime(new Date());
        return baseMapper.insert(schools);
    }

    @Override
    public List<String> getSchools(Long communityId) {
        QueryWrapper<ZfSchools> schoolsQueryWrapper = new QueryWrapper<>();
        schoolsQueryWrapper.lambda().eq(ZfSchools::getCommunityId,communityId);
      List<ZfSchools> schools =  baseMapper.selectList(schoolsQueryWrapper);
      List<String> aliIds = new ArrayList<>();
      if (schools.size()>0){
          for (ZfSchools sc :schools) {
              aliIds.add(sc.getAliEduId());
          }
          return aliIds;
      }
        return null;
    }

    @Override
    public List<CommunitySchoolsDto> getCommunitySchools(Long communityId) {
       return   baseMapper.getCommunitySchools(communityId);
    }

    @Override
    public int delByCommunityId(Long communityId) {
        QueryWrapper<ZfSchools> schoolsQueryWrapper = new QueryWrapper<>();
        schoolsQueryWrapper.lambda().eq(ZfSchools::getCommunityId,communityId);
        return  baseMapper.delete(schoolsQueryWrapper);
    }

    @Override
    public int delSchoolById(Long id) {
        QueryWrapper<ZfSchools> schoolsQueryWrapper = new QueryWrapper<>();
        schoolsQueryWrapper.lambda().eq(ZfSchools::getId,id);
        return  baseMapper.delete(schoolsQueryWrapper);
    }
}
